package com.bjzc.AndroidSDK;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.RS.AndroidSDK.HardwareCtrl;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;


public class MainActivity extends Activity {
    private Handler handler;
    private TextView Minfo;
    private TextView Wdata;
    private Chartwd chart1 = new Chartwd();
    private boolean isstop = true;
    private String fsl = "";
    private boolean issetfsl = false;
    private String worktag = "init";
    private List<HashMap<String, String>> wdlist = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//		chart1.buildchart(MainActivity.this);
        setTitle("温度test");
        Wdata = (TextView) findViewById(R.id.textdata);
        Minfo = (TextView) findViewById(R.id.fsl);
        Wdata.setText("");
        Wdata.setTextSize(Math.round(100 * Tools.get_adjust_ratio(this)));
        Minfo.setTextSize(Math.round(30 * Tools.get_adjust_ratio(this)));
        new MyThread().start();
//		HardwareCtrl.IRTinit(fsl);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ENTER) {
            if (!Wdata.getText().equals("")) {
                isstop = false;
                finish();
            }
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    class MyHandler extends Handler {
        public MyHandler(Looper looper) {
            super(looper);
        }

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String info = msg.getData().getString("name");
            Log.i("name = ", info);
            Wdata.setText(info);//info
            chart1.updateChartUI(Wdata.getText().toString());
            String fsl = msg.getData().getString("fsl");
            if (!fsl.equals("")) Minfo.setText(fsl);
        }
    }

    class MyThread extends Thread {
        public void run() {
            handler = new MyHandler(Looper.getMainLooper());
            while (isstop) {
                Bundle bd = new Bundle();//放在外面 有时更新ui会丢失数据
                if (worktag.equals("change")) {
                    HardwareCtrl.IRTclose();
                    worktag = "init";
                }
                ;
                if (worktag.equals("init")) {
                    fsl = HardwareCtrl.IRTinit(fsl);
                    bd.putString("fsl", fsl);
                    worktag = "read";
                } else
                    bd.putString("fsl", "");

                String ss = HardwareCtrl.IRTread();
                bd.putString("name", ss);
                Message msg = new Message();
                msg.setData(bd);
                handler.sendMessage(msg);
            }
            HardwareCtrl.IRTclose();
            System.out.println("Thread close!");
            System.exit(0);
        }
    }

}
